package com.cdream.basic.algorithm.leetcode.linkedlist;

/**
 * @author cdream
 * @date 6/12/21
 */
public class Question82 {
    public static void main(String[] args) {

        ListNode listNode = new ListNode(1);
        listNode.next = new ListNode(2);
        listNode.next.next = new ListNode(2);
        new Question82().deleteDuplicates(listNode);
    }

    public ListNode deleteDuplicates(ListNode head) {
        ListNode dummy = new ListNode(-1);

        ListNode lastUsefulNode = dummy;
        ListNode cur = head;
        while (cur != null) {
            int v = cur.val;
            // 如果有重复的
            if (cur.next != null && v == cur.next.val) {
                while (cur.next != null && cur.next.val == v) {
                    cur = cur.next;
                }
                cur = cur.next;
            } else {
                lastUsefulNode.next = cur;
                lastUsefulNode = cur;
                cur = cur.next;
                lastUsefulNode.next = null;
            }
        }
        return dummy.next;
    }
}